package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.ProductionArrange;
import com.mesSystem.entity.SalesOrder;
import com.mesSystem.service.impl.OrderService;
import com.mesSystem.service.impl.ProductionArrangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("order")
public class SalesOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    ProductionArrangeService productionArrangeService;

    /**
     * 创建订单
     * @param salesOrder
     * @return
     */
    @Transactional
    @PostMapping("increase")
    public Result increase(@RequestBody SalesOrder salesOrder) {
        log.info(salesOrder.toString());
        // 在产品定义的时候，便创建好产品 工序bom对应的数据
        orderService.save(salesOrder);
        productionArrangeService.increaseOnOrder(salesOrder.getSalesOrderId(), salesOrder.getSalesOrderId());
        return Result.ok("创建成功");
    }


    /**
     * 删除订单
     * TODO---根据订单删除相关内容
     * @param orderId
     * @return
     */
    @DeleteMapping("delete")
    public Result delete(Long orderId) {
        orderService.removeById(orderId);
        return Result.ok("删除成功");
    }

    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<SalesOrder> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        orderService.page(Page, new QueryWrapper<SalesOrder>().orderByAsc("sales_order_id"));

        // 从分页器对象中获取分页后的数据
        List<SalesOrder> list = Page.getRecords();

        log.info("被查询信息为：{}", list);

        // 返回分页结果
        return Result.ok(list);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody SalesOrder salesOrder) {
        orderService.updateById(salesOrder);
        log.info("update:{}", salesOrder);
        return Result.ok("更新成功");
    }
}
